package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.SmartRefreshViewModel;
import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.manhua.dal.service.UserCenterService;

import androidx.lifecycle.MutableLiveData;

/**
 * 首页->我的
 */
public class UserCenterViewModel extends SmartRefreshViewModel {

    private UserCenterService userCenterService = new UserCenterService(this);

    public UserCenterViewModel() {
        refreshUI();
    }

    // region    #业务数据
    public void refreshUserDetailData(){
        userCenterService.refreshUserDetailData();
    }

    // endregion #业务数据
    public void refreshUI() {
        refreshUserDataUI();
        refreshPanelDataUI();
        setMessageData(UserEntity.self.getMessageCount());
        setTaskData(UserEntity.self.getTaskRewardCount(), UserEntity.self.getTaskCount());
    }

    public void refreshUserDataUI() {
        boolean isLogin = UserEntity.self.isLogin();
        boolean isVip = UserEntity.self.isVip();

        set("refreshLayoutEnableRefresh", isLogin);
        set("vipHeadWearVisible", isVip ? View.VISIBLE : View.GONE);
        set("userHeadImage", UserEntity.self.getPortrait());
        set("userPanelPageNo", isLogin ? 1 : 0);
        set("userNickname", UserEntity.self.getName());
        set("coinBalance", String.valueOf(UserEntity.self.getCoinBalance()));
        set("beanBalance", String.valueOf(UserEntity.self.getBeanBalance()));
        set("vipInfoTipsText", isVip ? "有效期至：" + UserEntity.self.getShortVipExpirationTime() : "开通会员，享受更多福利！");
        set("goBuyMembershipBtnText", isVip ? "立即续费" : "￥18开通");
    }

    public void setTaskData(int waitReceiveTaskNumber, int unfinishedTaskNumber) {
        boolean isNotLogin = !UserEntity.self.isLogin();
        boolean isNotTask = (waitReceiveTaskNumber + unfinishedTaskNumber) <= 0;

        set("taskNumberVisible", (isNotLogin || isNotTask) ? View.GONE : View.VISIBLE);
        if(waitReceiveTaskNumber <= 0) {
            set("waitReceiveTaskNumber", "");
        } else {
            set("waitReceiveTaskNumber", waitReceiveTaskNumber > 99 ? "99+" : String.valueOf(waitReceiveTaskNumber));
        }
    }

    public void setMessageData(int messageNumber) {
        boolean isNotLogin = !UserEntity.self.isLogin();
        boolean isNotMessage = messageNumber <= 0;

        set("messageNumberVisible", (isNotLogin ||isNotMessage) ? View.GONE : View.VISIBLE);
        set("messageNumber", messageNumber > 99 ? "99+" : String.valueOf(messageNumber));
    }

    public void refreshPanelDataUI() {
        boolean isNotLogin = !UserEntity.self.isLogin();
        boolean isUpdateProtocol = DeviceConfigInfo.checkUpdateServiceProtocolVer() | DeviceConfigInfo.checkUpdatePrivacyProtocolVer();
        set("settingRedDotVisible", (isNotLogin || !isUpdateProtocol) ? View.GONE : View.VISIBLE);
    }

    // endregion #UI操作

    // region    #UI绑定

    public MutableLiveData<Boolean> getRefreshLayoutEnableRefresh() {
        return get("refreshLayoutEnableRefresh");
    }

    public MutableLiveData<Integer> getVipHeadWearVisible() {
        return get("vipHeadWearVisible");
    }

    public MutableLiveData<String> getUserHeadImage() {
        return get("userHeadImage");
    }

    public MutableLiveData<Integer> getUserPanelPageNo() {
        return get("userPanelPageNo");
    }

    public MutableLiveData<CharSequence> getUserNickname() {
        return get("userNickname");
    }

    public MutableLiveData<CharSequence> getCoinBalance() {
        return get("coinBalance");
    }

    public MutableLiveData<CharSequence> getBeanBalance() {
        return get("beanBalance");
    }

    public MutableLiveData<CharSequence> getVipInfoTipsText() {
        return get("vipInfoTipsText");
    }

    public MutableLiveData<CharSequence> getGoBuyMembershipBtnText() {
        return get("goBuyMembershipBtnText");
    }

    public MutableLiveData<Integer> getTaskNumberVisible() {
        return get("taskNumberVisible");
    }

    public MutableLiveData<CharSequence> getWaitReceiveTaskNumber() {
        return get("waitReceiveTaskNumber");
    }

    public MutableLiveData<Integer> getMessageNumberVisible() {
        return get("messageNumberVisible");
    }

    public MutableLiveData<CharSequence> getMessageNumber() {
        return get("messageNumber");
    }

    public MutableLiveData<Integer> getSettingRedDotVisible() {
        return get("settingRedDotVisible");
    }

    // endregion #UI绑定

}
